using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;

namespace KFGame
{
    ///共享信息
    public class KFShereNetwork : MonoBehaviour
    {


        public static string iniPath = "/sharedatas";
        public static string group = "KFINFO";
        public bool enter = false;
        //地址
        public string host;

        public int port;

        //组名
        public string groupname;
        //自身名
        public string username;
        //成员
        public string groupusers;

        //投币
        public int incoin = 1;
        //开门
        public int indoor = 2;


        ///消息队队
        public List<int> messages = new List<int>();


        KFSocketClient client;


        string requurl = "";
        IEnumerator requets()
        {
            var url = requurl;

            while (true)
            {
                var www = UnityWebRequest.Get(url);
                yield return www.SendWebRequest();

                if (www.isHttpError || www.isNetworkError)
                {
                    Debug.Log(www.error);
                }
                else
                {
                    passjson(www.downloadHandler.text);
                }
            }
        }



        void passjson(string txt)
        {

        }

        public static KFShereNetwork network;
        public static KFShereNetwork GetSHareNetwrok()
        {
            if (network == null)
            {
                var o = new GameObject("ShereNetwork");
                network = o.AddComponent<KFShereNetwork>();
                DontDestroyOnLoad(o);
            }

            return network;
        }


        public int readDatas()
        {
            if (messages.Count == 0)
                return 0;
            int v = messages[0];
            messages.RemoveAt(0);
            return v;
        }
        void readConfig()
        {

            INIParser iNIParser = KFFile.InitParser(iniPath);
            enter = iNIParser.ReadValue(group, "enter", false);
            host = iNIParser.ReadValue(group, "host", "dropball.ahnss.vip");
            port = iNIParser.ReadValue(group, "port", 6902);
            groupname = iNIParser.ReadValue(group, "groupname", "");
            username = iNIParser.ReadValue(group, "username", "");
            groupusers = iNIParser.ReadValue(group, "groupusers", "");


            requurl = host + "?" + "groupname=" + groupname + "&username=" + username + "&groupusers=" + groupusers;
        }

        void OnEnable()
        {

        }

        void runserver()
        {
            client = KFSocketClient.instantiation;
            client.OnAction = recMessage;
            if (!client.IsConnected())
            {
                if (enter)
                    client.ConnectServer(host, port);

            }

        }
       
        void recMessage(string datas)
        {


            string[] md = KFSocketClient.toDatas(datas);
            if (md[0] == "0" && md[1] == "0")
            {

                List<string> group = new List<string>();
                group.Add("0");
                group.Add("0");
                group.Add(groupname);

                string msg = KFSocketClient.toMessages(group.ToArray());
                client.SendMessage(msg);
            }

            try
            {
                if (md[0] == "1")
                {

                    int a = Convert.ToInt32(md[2]);

                    messages.Add(a);
                }
            }
            catch (Exception )
            {

            }
        }
        void connent_user()
        {
            List<string> group = new List<string>();
            group.Add("0");
            group.Add(groupname);
            group.Add(username);
            string msg = KFSocketClient.toMessages(group.ToArray());
            KFSocketClient.instantiation.SendMessage(msg);
        }

        public void sendincon()
        {
            if (!enter)
                return;

            List<string> group = new List<string>();
            group.Add("1");
            group.Add("0");
            group.Add(Convert.ToString(incoin));
            string msg = KFSocketClient.toMessages(group.ToArray());
            KFSocketClient.instantiation.SendMessage(msg);
        }
        public void senddoor()
        {
            if (!enter)
                return;

            List<string> group = new List<string>();
            group.Add("1");
            group.Add("1");
            group.Add(Convert.ToString(indoor));
            string msg = KFSocketClient.toMessages(group.ToArray());
            KFSocketClient.instantiation.SendMessage(msg);
        }

        void Awake()
        {
            readConfig();
        }
        void Start()
        {
            runserver();
        }

        // Update is called once per frame
        void Update()
        {

        }

        void OnApplicationQuit()
        {
            client.Close();
        }
    }
}
